<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>11.17.JS对象-内置对象-添加或删除数组元素</title>
    <script>
        // 添加或删除数组元素

        // 从第 2 位开始删除 0 个元素，插入“x”
        var arr = ["1", "2", "3", "4"];
        var removed = arr.splice(2, 0, "x"); //arr: ["1", "2", "x", "3", "4"]
        console.log(arr);
        console.log('---------------------------');
        // 从第 2 位开始删除 0 个元素，插入“x” 和 "y"
        var arr = ['1', '2', '3', '4'];
        var removed = arr.splice(2, 0, 'x', 'y');//  arr: ["1", "2", "x", "y", "3", "4"]
        console.log(arr);
        console.log('---------------------------');
        // 从第 3 位开始删除 1 个元素
        var arr = ['1', '2', 'x', '3', '4'];
        var removed = arr.splice(3, 1);//  arr: ["1", "2", "x", "4"]
        console.log(arr);
        console.log('---------------------------');
        // 从第 2 位开始删除 1 个元素，插入“e”
        var arr = ['1', '2', 'x', '4'];
        var removed = arr.splice(2, 1, "e");//  arr: ["1", "2", "e", "4"]
        console.log(arr);
        console.log('---------------------------');
        // 从第 0 位开始删除 2 个元素，插入"q"、"w"和"e"
        var arr = ['1', '2', 'e', '4'];
        var removed = arr.splice(0, 2, 'q', 'w', 'e');//  arr: ["q", "w", "e", "e", "4"]
        console.log(arr);
        console.log('---------------------------');
        // 从第 2 位开始删除 2 个元素
        var arr = ['q', 'w', 'e', 'e', '4'];
        var removed = arr.splice(arr.length - 3, 2);//  arr: ["q", "w", "4"]
        console.log(arr);
        console.log('---------------------------');
        // 从倒数第 2 位开始删除 1 个元素
        var arr = ['1', '2', '3', '4'];
        var removed = arr.splice(-2, 1);//  arr: ["1", "2", "4"]
        console.log(arr);
        console.log('---------------------------');
        // 从第 2 位开始删除所有元素
        var arr = ['1', '2', '3', '4'];
        var removed = arr.splice(2);//  arr: ["1", "2"]
        console.log(arr);
        console.log('---------------------------');

    </script>
</head>

<body>

</body>

</html>